package jp.aist.gtrc.plus.scheduler.node;

import java.util.Comparator;
import java.util.Map;;

import jp.aist.gtrc.plus.scheduler.specific.pbs.status.PBSNodeStatus;
import jp.aist.gtrc.plus.scheduler.status.NodeStatus;
import jp.aist.gtrc.plus.scheduler.util.SortKey;


public enum ProvisionSortKey implements SortKey<NodeInfo> {

	NodeName(
		new Comparator<NodeInfo>() {
			public int compare(NodeInfo arg0, NodeInfo arg1) {
				return arg0.getName().compareTo(arg1.getName());
			}
		}),

	Type(
		new Comparator<NodeInfo>() {
			public int compare(NodeInfo arg0, NodeInfo arg1) {
				NodeStatus n0 = arg0.getStatus();
				NodeStatus n1 = arg1.getStatus();
				if ((n0 instanceof PBSNodeStatus)
						&& (n1 instanceof PBSNodeStatus)) {
					PBSNodeStatus p0 = (PBSNodeStatus)n0;
					PBSNodeStatus p1 = (PBSNodeStatus)n1;
                                        int v0 = 0;
                                        int v1 = 0;
                                        Map<String,String> propMap1 = p0.getAttributeMap();
                                        if ( (propMap1 != null) && (propMap1.get("provisioned") != null) ) {
                                           v0 = 1;
                                        }
                                        Map<String,String> propMap2 = p1.getAttributeMap();
                                        if ( (propMap2 != null) && (propMap2.get("provisioned") != null) ) {
                                           v1 = 1;
                                        }

					return v0 - v1;
				} else {
					return 0;
				}
			}
		});


	private final Comparator<NodeInfo> comp;

	ProvisionSortKey(Comparator<NodeInfo> comp) {

		this.comp = comp;

	}

	public Comparator<NodeInfo> getComparator(){

		return comp;

	}

}
